﻿Public Class frmHoaDonNhap
    Dim ma As Integer
    Private Sub frmHoaDonNhap_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadDanhSachLenLuoi()
        LoadTenDaiLy()
    End Sub
    Public Sub LoadDanhSachLenLuoi()
        dgvDanhSachMatHang.DataSource = MatHangDAO.selectall()
    End Sub
    Public Sub LoadTenDaiLy()
        cmbTenDaiLy.DataSource = NhaCungCapDAO.selectALL()
        cmbTenDaiLy.DisplayMember = "TENNHACUNGCAP"
        cmbTenDaiLy.ValueMember = "MANHACUNGCAP"
    End Sub
    Public Sub LoadTongGiaTriHoaDon()
        Try
            lbTongTien.Text = "Tong gia tri: " & HoaDonBanDAO.TotalValue(txtMaHoaDon.Text)
        Catch ex As Exception
            lbTongTien.Text = "Tong gia tri: 0d"
        End Try
    End Sub
    Public Sub HienThi()
        If dgvDanhSachMatHang.SelectedRows.Count > 0 Then
            txtMaMatHang.Text = dgvDanhSachMatHang.SelectedRows(0).Cells(0).Value.ToString()
            txtTenMatHang.Text = dgvDanhSachMatHang.SelectedRows(0).Cells(1).Value.ToString()
            txtSoLuongNhap.Text = dgvDanhSachMatHang.SelectedRows(0).Cells(5).Value.ToString()
        End If
    End Sub

    Private Sub dgvDanhSachMatHang_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvDanhSachMatHang.SelectionChanged
        HienThi()
    End Sub

    Private Sub btnLapPhieu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLapPhieu.Click
        'insert vao bang hoa don ban
        Dim hd As New HoaDonNhapDTO
        hd.MaHoaDonNhap = txtMaHoaDon.Text
        hd.MaNhaCungCap = cmbTenDaiLy.SelectedValue
        'hd.NgayLapHoaDonBan = DateTimePicker
        'hd.TriGiaHoaDonNhap = HoaDonNhapDAO.TotalValue(txtMaHoaDon.Text)
        LoadTongGiaTriHoaDon()
        loadHoaDon(txtMaHoaDon.Text)
        HoaDonNhapDAO.Insert(hd)
    End Sub
    Public Sub loadHoaDon(ByVal mahd As String)
        dgvDanhSachChiTietPhieu.DataSource = HoaDonNhapDAO.selecChiTiet(mahd)
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Dim cthd As New ChiTietPhieuNhapDTO
        cthd.SoLuongNhap = txtSoLuongNhap.Text
        cthd.DonGiaNhap = txtDonGia.Text
        cthd.MaMatHang = txtMaMatHang.Text
        cthd.MaChiTietHoaDonNhap = ChiTietPhieuNhapDAO.MaTuTang()
        cthd.MaHoaDonNhap = txtMaHoaDon.Text
        ChiTietPhieuNhapDAO.insert(cthd)
        loadHoaDon(txtMaHoaDon.Text)
        LoadTongGiaTriHoaDon()
        ' update tong gia tri hoa don
        updateTongTien()
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        ChiTietPhieuNhapDAO.delete(ma)
        loadHoaDon(txtMaHoaDon.Text)
        LoadTongGiaTriHoaDon()
    End Sub

    Private Sub dgvDanhSachChiTietPhieu_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvDanhSachChiTietPhieu.SelectionChanged
        If dgvDanhSachChiTietPhieu.SelectedRows.Count > 0 Then
            ma = dgvDanhSachChiTietPhieu.SelectedRows(0).Cells(0).Value.ToString()
        End If
    End Sub
    Public Sub updateTongTien()
        Dim hd As New HoaDonNhapDTO
        hd.MaHoaDonNhap = txtMaHoaDon.Text
        hd.TriGiaHoaDonNhap = HoaDonNhapDAO.TotalValue(txtMaHoaDon.Text)
        HoaDonNhapDAO.Update(hd)
    End Sub
End Class